package br.com.locanet.view.impl;

import java.sql.SQLException;

import javax.faces.bean.ManagedBean;

import br.com.locanet.bean.impl.CopiaFilme;
import br.com.locanet.bean.impl.Filme;
import br.com.locanet.exception.InvalidAttributeException;
import br.com.locanet.service.impl.CopiaFilmeService;
import br.com.locanet.service.impl.FilmeService;
import br.com.locanet.view.AbstractManagedBean;

@ManagedBean(name="copiaFilmeBean")
public class CopiaFilmeBean extends AbstractManagedBean<CopiaFilme, CopiaFilmeService>{
	
	public void select() throws Exception {
		setEntities(getEntityService().select(getEntity()));
	}
	public String update() throws Exception {
		doSetFilmes();
		getEntityService().update(getEntity());
		select();
		return UPDATE;
	}
	public void get_update()throws Exception{
		doSetFilmes();
		if (getEntity().getId()==null) throw new InvalidAttributeException("idCopiaFilmeNaoInformado");
		
		CopiaFilme copiaFilme = getEntityService().selectById(getEntity());
		
		getRequest().setAttribute("copiaFilme", copiaFilme);
	}
	private void doSetFilmes() throws SQLException,ClassNotFoundException{
		Filme filme = new Filme();
		filme.setStatus(1);
		getRequest().setAttribute("filmes", getFilmeService().select(filme));
	}
	private FilmeService getFilmeService(){
	  return new FilmeService();
	}
	public String insert() throws Exception {
		doSetFilmes();
		getEntity().setStatus(1);
		getEntityService().insert(getEntity());
		select();
		return INSERT;
	}
	public void get_insert() throws Exception {
		doSetFilmes();
	}
	@Override
	public String remove() throws Exception {
		doExclusaoLogica(getEntity());
		select();
		return REMOVE;
	}
	private void doExclusaoLogica(CopiaFilme copia)throws Exception{
		copia=getEntityService().selectById(copia);
		copia.setStatus(copia.getStatus()==1?0:1);
		getEntityService().update(copia);
		
	}
}
